<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <h1>过期</h1>
  </body>
  <script>
    const Storage = {
      set(key, value, seconds) {
        const data = { value };
        console.log(data);
        // 如果有过期时间，则设置过期时间 expire
        if (seconds) {
          data.expire = Date.now() + seconds * 1000;
        }
        localStorage.setItem(key, JSON.stringify(data));
      },
      get(key) {
        const data = JSON.parse(localStorage.getItem(key));
        if (data) {
          // 如果设置了过期时间，则判断是否过期
          if (data.expire && data.expire < Date.now()) {
            // 过期则删除
            return data.value;
          }
          localStorage.removeItem(key);
        }
      },
    };
    Storage.set("name", "zhangsan", 10);
    
  </script>
</html>
